﻿using System;
using BasecampRestAPI;
using NUnit.Framework;
using TestBasecampRestAPI.Fakes;
using TestBasecampRestAPI.TestMoveToDoList;

namespace TestBasecampRestAPI
{
	[TestFixture]
	public class TestListMover
	{
		ListMover _mover;
		FakeProject _targetProject;

		[SetUp]
		public void SetUp()
		{
			_targetProject = new FakeProject();
			_mover = ListMover.GetInstance(_targetProject);
		}

		[Test]
		public void Construct()
		{
			Assert.IsNotNull(_mover);
		}

		[Test]
		public void CopyMilestone()
		{
			FakeProject sourceProject = new FakeProject
			{
				Id = 718777
			};
			FakeMilestone sourceMilestone = new FakeMilestone
			{
				Id = 10808780,
				Deadline = new DateTime(2009, 12, 03),
				Title = "Milestone 1",
				WantsNotification = true,
				Project = sourceProject
			};
			_targetProject.CreateMilestoneFakeResult = new FakeMilestone
			{
				Id = sourceMilestone.Id*2,
				Title = sourceMilestone.Title,
				Deadline = sourceMilestone.Deadline,
				WantsNotification =  sourceMilestone.WantsNotification,
				Project = _targetProject
			};

			IMilestone targetMilestone = _mover.CopyMilestone(sourceMilestone);

			Assert.IsTrue(_targetProject.CreateMilestoneCalled);
			Assert.AreEqual(sourceMilestone.Title, _targetProject.CreateMilestoneLastTitle);
			Assert.AreEqual(sourceMilestone.Deadline, _targetProject.CreateMilestoneLastDeadline);
			Assert.AreEqual(sourceMilestone.WantsNotification, _targetProject.CreateMilestoneLastNotify);
			Assert.AreNotEqual(targetMilestone.Id, sourceMilestone.Id);
		}

		[Test]
		public void CopyMilestoneFromProjectToSameProject()
		{
			FakeMilestone sourceMilestone = new FakeMilestone
			{
				Id = 10808780,
				Deadline = new DateTime(2009, 12, 03),
				Title = "Milestone 1",
				WantsNotification = true,
				Project = _targetProject
			};

			IMilestone targetMilestone = _mover.CopyMilestone(sourceMilestone);

			Assert.IsFalse(_targetProject.CreateMilestoneCalled);
			Assert.IsNotNull(targetMilestone);
			Assert.AreEqual(targetMilestone.Id, sourceMilestone.Id);
		}

		[Test]
		public void CopyToDoList()
		{
			FakeProject sourceProject = new FakeProject
			{
				Id = 878878
			};
			FakeMilestone milestone = new FakeMilestone
			{
				Id = 10808780,
				Deadline = new DateTime(2009, 12, 03),
				Title = "Milestone 1",
				WantsNotification = true,
				Project = sourceProject
			};
			FakeMilestone targetMilestone = new FakeMilestone
			{
				Id = milestone.Id*2,
				Deadline = milestone.Deadline,
				Title = milestone.Title,
				WantsNotification = milestone.WantsNotification,
				Project = _targetProject
			};
			_targetProject.CreateMilestoneFakeResult = targetMilestone;
			FakeToDoList sourceList = new FakeToDoList
			{
				Id = 5454,
				Name = "Foo",
				Description = "The list for Foo stuff.",
				Milestone = milestone,
				Private = false,
				Tracked = true,
				Items = new IToDoListItem[0]
			};
			_targetProject.CreateToDoListFakeResult = new FakeToDoList
			{
				Id = sourceList.Id*2,
				Name = sourceList.Name,
				Description = sourceList.Description,
				Private = sourceList.Private,
				Tracked = sourceList.Tracked
			};

			IToDoList targetList = _mover.CopyToDoList(sourceList);

			Assert.IsTrue(_targetProject.CreateToDoListCalled);
			Assert.AreEqual(sourceList.Name, _targetProject.CreateToDoListLastName);
			Assert.AreEqual(sourceList.Description, _targetProject.CreateToDoListLastDescription);
			Assert.AreEqual(targetMilestone, _targetProject.CreateToDoListLastMilestone);
			Assert.AreEqual(sourceList.Private, _targetProject.CreateToDoListLastPrivateList);
			Assert.AreEqual(sourceList.Tracked, _targetProject.CreateToDoListLastTrackedList);
			Assert.AreNotEqual(targetList.Id, sourceList.Id);
		}

		[Test]
		public void CopyToDoListItems()
		{
			FakePerson creator = new FakePerson { Id = 3113 };
			FakeToDoListItem item1 = new FakeToDoListItem
			{
				Id = 1414,
				Content = "This is some stuff I did.",
				Creator = creator,
				Completed = false,
				ResponsibleParty = creator,
				TimeEntries = new ITimeEntry[0]
			};
			FakeToDoListItem item2 = new FakeToDoListItem
			{
				Id = 1818,
				Content = "This is some other stuff I did.",
				Creator = creator,
				Completed = true,
				ResponsibleParty = creator,
				TimeEntries = new ITimeEntry[0]
			};
			FakeToDoList sourceList = new FakeToDoList
			{
				Id = 3232,
				Name = "Foo",
				Description = "List of Foo stuff.",
				Items = new IToDoListItem[] { item1, item2 }
			};

			FakeToDoListItem copy1 = CloneItem(item1);
			FakeToDoListItem copy2 = CloneItem(item2);
			FakeToDoList cloneList = new FakeToDoList
			{
				Id = sourceList.Id*2,
				Name = sourceList.Name,
				Description = sourceList.Description,
				Items = new IToDoListItem[] { copy1, copy2 }
			};
			_targetProject.CreateToDoListFakeResult = cloneList;
			cloneList.CreateItemFakeResultForContent("This is some stuff I did", copy1);
			cloneList.CreateItemFakeResultForContent("This is some other stuff I did.", copy2);

			IToDoList newList = _mover.CopyToDoList(sourceList);
			Assert.IsTrue(cloneList.CreateItemCalled);
			IToDoListItem[] items = newList.Items;
			Assert.AreEqual(2, items.Length);
			Assert.IsTrue(copy2.CompleteCalled);
		}

		private static FakeToDoListItem CloneItem(IToDoListItem item)
		{
			return new FakeToDoListItem
			{
				Id = item.Id*2,
				Content = item.Content,
				Creator = item.Creator,
				Completed = item.Completed,
				ResponsibleParty = item.ResponsibleParty
			};
		}
	}
}
